#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
using namespace std;

const int maxt = 1024;

int main()
{
	int t,kcase=0;
	while(scanf("%d",&t)==1 && t){
		printf("Scenario #%d\n",++kcase);
		//recoder number of the team
		map<int,int> team;
		int n,x;
		for(int i=0;i<t;i++){
			scanf("%d",&n);
			while(n--){		scanf("%d",&x); team[x] = i;}
		}
		//simulator
		queue<int> q,qt[maxt];
		char cmd[10];
		int num;
		while(1){
			scanf("%s",cmd);
			if(cmd[0] == 'E'){
				scanf("%d",&num);
				int t = team[num];
				if(qt[t].empty())
					q.push(t);
				qt[t].push(num);
			}else if(cmd[0] == 'D'){
				int t=q.front();
				printf("%d\n",qt[t].front());
				qt[t].pop();
				if(qt[t].empty())
					q.pop();
			}else if(cmd[0] == 'S')	break;
		}
		printf("\n");
	}
	return 0;
}
